var datatable = null;
var torrents = [];

$(document).ready(function() {

    datatable = $('#datatable').dataTable({
    	'bJQueryUI': true,
    	'sPaginationType': 'full_numbers',
		'sDom': 'T<"clear"><"fg-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix"lfr>t<"fg-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix"ip>',
		'oTableTools': {
			'sRowSelect': 'multi',
			'aButtons': [ 'select_all', 'select_none',
			             {
			                 'sExtends': 'text',
			                 'sButtonText': 'Expand',
			                 'fnClick': function(button, config) {
			                     $('#datatable tbody tr.DTTT_selected td:not(.details,.expand)').each(function() {
			                         if(!datatable.fnIsOpen($(this).parents('tr')[0])) {
			                             $(this).dblclick();
			                         }
			                     })
			                 }
			             },
			             {
			                 'sExtends': 'text',
			                 'sButtonText': 'Collapse',
			                 'fnClick': function(button, config) {
			                     $('#datatable tbody tr.DTTT_selected td:not(.details,.expand)').each(function() {
			                         if(datatable.fnIsOpen($(this).parents('tr')[0])) {
			                             $(this).dblclick();
			                         }
			                     })
			                 }
			             },
			             {
			                 'sExtends': 'text',
			                 'sButtonText': 'Pause',
			                 'fnClick': function(button, config) {
			                     $('#datatable tbody tr.DTTT_selected td:not(.details,.expand)').each(function() {
			                         var nTr = $(this).parents('tr')[0];
			                         torrents[datatable.fnGetPosition(nTr)].pause();
			                     })
			                 }
			             },
			             {
			                 'sExtends': 'text',
			                 'sButtonText': 'Play',
			                 'fnClick': function(button, config) {
			                     $('#datatable tbody tr.DTTT_selected td:not(.details,.expand)').each(function() {
			                         var nTr = $(this).parents('tr')[0];
			                         torrents[datatable.fnGetPosition(nTr)].play();
			                     })
			                 }
			             },
			           ]
		},
		'aoColumns': [
			{ 'mData': 'unique_id' },
			{ 'mData': 'name' },
			{
			  'mData': 'added_on',
			  'bSearchable': false,
			  'sType': 'date',
			},
			{
			  'mData': 'progressbar',
			  'bSearchable': false,
			},
			{
			  'mData': 'size',
			  'sClass': 'align-right',
			},
			{
			  'mData': 'upload_rate',
			  'sClass': 'align-right',
			},
			{
			  'mData': 'download_rate',
			  'sClass': 'align-right',
			},
			{ 'mData': 'location' },
		],
    });
    datatable.fnSort([[0,'desc']]);
    
	$('#accordion').accordion({
		heightStyle: 'content',
		active: 1,
	});
	
    $('#torrent-link').focus(function() {
    	$('#accordion').accordion('activate', 1);
    });
    
    $('#add-torrent').submit(function() {
    	$('#accordion').accordion('activate', 1);
        
        var link = $('#torrent-link').val().trim();
        $('#torrent-link').val('');
        
        if(link != '') {
        	torrents.push(new Torrent(datatable, link));
        }
    	
    	return false;
    });
    
});